package edu.java.texbooks.scjp.collections.test02;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class TestArrayList {

	static String string1 = "grozde";
	static String string2 = "krusha";
	static String string3 = "sliva";
	static String string4 = "hinap";
	static String string5 = "shipka";
	static String string6 = "yagoda";

	public static void main(String[] args) {

		// test01();

		// test02();

		test03();
	}

	public static void test01() {
		List<String> arrayListStrings = new ArrayList<String>();
		System.out
				.println("Size of the array list: " + arrayListStrings.size());
		String myString = "kiho";
		String otherString = "koshi";

		arrayListStrings.add(myString);
		System.out.println("The element on position "
				+ arrayListStrings.indexOf(myString) + " is: " + myString);

		arrayListStrings.add(otherString);
		System.out
				.println("Size of the array list: " + arrayListStrings.size());
		System.out.println(arrayListStrings);

		String winter = "snow";
		boolean elementCheck = arrayListStrings.contains(winter);
		System.out.println("Does the ArrayList consist the string \" " + winter
				+ "\": " + elementCheck);

		arrayListStrings.add(winter);
		System.out.println("Check again the if it contains the string: "
				+ arrayListStrings.contains(winter));

		String string1 = "mushmorok";
		arrayListStrings.add(string1);
		System.out.println("New size of the array: " + arrayListStrings.size());

		System.out.println(arrayListStrings);
		arrayListStrings.add(1, string2);
		System.out.println(arrayListStrings);

		System.out.println("\n---------------------------------------");
		List<String> myArrayListStrings = new ArrayList<String>(2);
		System.out.println("myArrayListStrings.size()= "
				+ myArrayListStrings.size());
		System.out.println("Is myArrayListStrings empty: "
				+ myArrayListStrings.isEmpty());

		System.out.println(myArrayListStrings);
		myArrayListStrings.add(0, string4);
		System.out.println(myArrayListStrings);

		// subtest 1
		// myArrayListStrings.addAll(myArrayListStrings.size(),
		// arrayListStrings);
		// System.out.println(myArrayListStrings);
		// myArrayListStrings.removeAll(arrayListStrings);
		// System.out.println(myArrayListStrings);

		// subtest 2
		// myArrayListStrings.addAll(arrayListStrings);
		// System.out.println(myArrayListStrings);
		// myArrayListStrings.add(string5);
		// System.out.println(myArrayListStrings);
		// myArrayListStrings.removeAll(arrayListStrings);
		// System.out.println(myArrayListStrings);

		// subtest 3
		System.out.println("\n---------------------------------------");
		myArrayListStrings.add(string5);
		List<String> copyStrings = new ArrayList<String>();
		copyStrings.addAll(myArrayListStrings);

		System.out.println("Print copy of myArrayListString: " + copyStrings);
		copyStrings.addAll(copyStrings);
		System.out.println(copyStrings);

	}

	public static void test02() {
		List<String> fruits = new ArrayList<String>();
		fruits.add(0, string1);
		fruits.add(string2);
		fruits.add(string3);
		System.out.println("fruits: \t" + fruits.toString());

		List<String> specialFruits = new ArrayList<String>(fruits);
		System.out.println("specialFruits: \t" + specialFruits);

		specialFruits.clear();
		System.out.println("specialFruits after deletion: " + specialFruits);
		specialFruits.addAll(specialFruits);
		System.out.println("specialFruits: \t" + specialFruits);

		specialFruits.addAll(fruits);
		specialFruits.add(2, string4);
		System.out.println("specialFruits: \t" + specialFruits);
		specialFruits.add(0, string5);

		System.out.println("\n---------------------------------");
		System.out.println("fruits: \t" + fruits);
		System.out.println("specialFruits: \t" + specialFruits);

		String someFruit = fruits.get(2);
		System.out.println("Some fruit: \t" + someFruit);
		System.out.println("\nCount of fruits in specialFruits: "
				+ specialFruits.size());

		int position = specialFruits.size() - 2;
		System.out.println("Fruit on position " + position + " : \t"
				+ specialFruits.get(position));

		// System.out.println("Is empty specialFruits: " +
		// specialFruits.isEmpty());
		someFruit = specialFruits.get(position);
		System.out.println("Does specialFruits contain " + someFruit + ":  "
				+ specialFruits.contains(someFruit));
		someFruit = "tutiFruty";
		System.out.println("Does specialFruits contain " + someFruit + ":  "
				+ specialFruits.contains(someFruit));

		System.out.println("\n---------------------------------");
		specialFruits.add(new String("hinap"));
		specialFruits.add(string5);
		specialFruits.add(string4);
		System.out.println("specialFruits: \t" + specialFruits);
		System.out.println("First index of fruit " + string4 + ": "
				+ specialFruits.indexOf(string4));

		// subtest 1.1
		specialFruits.remove("hinap");
		System.out.println("specialFruits: \t" + specialFruits);

		// subtest 1.2
		// while(specialFruits.remove("hinap")){
		// }
		// System.out.println("specialFruits: \t" + specialFruits);

		System.out.println("Last index of fruit " + string4
				+ " in the revised specialFruits: "
				+ specialFruits.lastIndexOf(string4));

		specialFruits.remove(6);
		System.out.println("specialFruits: \t" + specialFruits);

		System.out.println("\n---------------------------------");
		specialFruits.add(6 + "");
		System.out.println("specialFruits: \t" + specialFruits);
		specialFruits.add(6, "yagoda");
		System.out.println("specialFruits: \t" + specialFruits);
		// specialFruits.remove(6);
		specialFruits.remove(6 + "");
		System.out.println("specialFruits: \t" + specialFruits);

		System.out.println("\n---------------------------------");
		// ASK
		// ((ArrayList<String>)specialFruits).removeRange(1,2);

		specialFruits.set(2, string6);
		System.out.println("specialFruits: \t" + specialFruits);
		String myFruit = specialFruits.get(2);
		System.out.println("I like " + myFruit);

		// subtest 2.1
		// Object[] specialFruitsArray= specialFruits.toArray();
		// System.out.println(Arrays.toString(specialFruitsArray));

		// subtest 2.2
		// System.out.println("\n---------------------------------");
		// // Object[] myObjectFruitArray = new Object[0];
		// // Object[] myObjectFruitArray = new Object[specialFruits.size()-1];
		// Object[] myObjectFruitArray = new Object[specialFruits.size()];
		// Object[] objectFruitArray =
		// specialFruits.toArray(myObjectFruitArray);
		// System.out.println(Arrays.toString(objectFruitArray));
		// System.out.println(myObjectFruitArray == objectFruitArray);

		// subtest 2.3
		// Object[] objectArray = new Object[specialFruits.size()];
		// objectArray = specialFruits.toArray(objectArray);
		// System.out.println(Arrays.toString(objectArray));

		System.out.println("\n---------------------------------");
		List<Integer> myIntegers = new ArrayList<Integer>();
		for (int i = 1; i <= 10; i++) {
			myIntegers.add(i);
		}
		System.out.println(myIntegers);

		Object[] myObjectInteresArray = new Object[myIntegers.size()];
		myObjectInteresArray = myIntegers.toArray(myObjectInteresArray);
		System.out.println("Array of objects: \t"
				+ Arrays.toString(myObjectInteresArray));

		Integer[] myIntegersArray = new Integer[myIntegers.size()];
		myIntegersArray = myIntegers.toArray(myIntegersArray);
		System.out.println("Array of integers: \t"
				+ Arrays.toString(myIntegersArray));

		System.out.println("\n---------------------------------");
		System.out.println("The size of myIntegers: " + myIntegers.size());

		// ASK --> why I can't get methods ensureCapacity() and trimToSize().
		// And also the protected method removeRange()
		myIntegers.add(null);
		System.out.println(myIntegers);
		System.out.println("The size of myIntegers: " + myIntegers.size());

	}

	public static void test03() {
		List<String> fruits = new ArrayList<String>();
		fruits.add(string1);
		fruits.add(string2);
		fruits.add(string3);
		fruits.add(string4);
		fruits.add(string5);
		fruits.add(string6);

		System.out.println(fruits);
		Collections.sort(fruits);
		System.out.println(fruits);
	}
}
